﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.VehicleReport.Model;
using Yipusoft.VehicleReport.BLL;
using System.Data;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;

public partial class Police_SecurityEvaluate_MendRecordView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Guid id = new Guid(Request["id"]);
            int flag = Convert.ToInt32(Request["flag"]);
            DataSet ds = MendRecord.GetAllMendRecords(1, 0, "id='" + id + "'");
            DataRow dr = ds.Tables[0].Rows[0];
            string DepartmentId = dr["DepartmentId"].ToString();
            Guid Vid = new Guid(dr["Vid"].ToString());
            DateTime MendTime = Convert.ToDateTime(dr["MendTime"].ToString());
            DateTime FinishTime = Convert.ToDateTime(dr["FinishTime"].ToString());
            string VehicleName = dr["VehicleName"].ToString();
            //int flag = Convert.ToInt32(dr["flag"]);
            //Guid DeviceId = new Guid(dr["DeviceId"].ToString());
            DepartmentInfo department = Department.GetDepartment(DepartmentId);
            if (department != null)
            {
                lblUnitName.Text = department.FullName;
            }
            lblMendTime.Text = MendTime == DateTime.MaxValue ? "" : MendTime.ToString("yyyy-MM-dd");
            lblFinishTime.Text = FinishTime.Year == 9999 ? "" : FinishTime.ToString("yyyy-MM-dd");

            

            VehicleInfo vehicle = Vehicle.GetDetail(Vid);
            if (vehicle == null)
            {
                if (VehicleName == "")
                {
                    VehicleName = "此车辆已删除";
                }
                else
                {
                    VehicleName += "[已删除]";
                }
            }
            else
            {
                VehicleName = vehicle.VehicleName + "【" + vehicle.NumberPlate + "】";
            }

            UserInfo user= Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            if (user.DepartmentID.Substring(0, 4) == "1309")
            {
                ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" Left(DepartmentID,4)='1309'");
                ddlDepartment.DataTextField = "ShortName";
                ddlDepartment.DataValueField = "DepartmentID";
                ddlDepartment.DataBind();
                ddlDepartment.Visible = true;
                MendDeviceInfo menddevice;
                MendRecordInfo record;
                if (flag == 1)
                {
                    menddevice = MendDevice.GetDetail(id);

                    if (menddevice != null)
                    {
                       

                        if (menddevice.ZhongdOk == null || menddevice.ZhongdOk == "")
                        {
                            btnZhongdui.Visible = true;
                            txtZhongdui.Visible = true;
                            txtZhongdui.Text = DateTime.Now.ToString("yyyy-MM-dd");
                            txtUserNameZhongDui.Visible = true;
                        }

                        if (string.IsNullOrEmpty(menddevice.DadOk.Trim()))
                        {
                            btnDaDui.Visible = true;
                            txtDaDui.Visible = true;
                            txtDaDui.Text = DateTime.Now.ToString("yyyy-MM-dd");
                            txtUserNameDaDui.Visible = true;
                        }

                        if (menddevice.ZhidOk == null || menddevice.ZhidOk == "")
                        {

                            btnZhiDui.Visible = true;
                            txtDateZhiDui.Visible = true;
                            txtDateZhiDui.Text = DateTime.Now.ToString("yyyy-MM-dd");
                            txtUserNameZhiDui.Visible = true;
                        }

                    }
                }
                if (flag == 0)
                {
                    record = MendRecord.GetDetail(id);

                    if (record != null)
                    {                     

                        if (record.ZhongdOk == null || record.ZhongdOk == "")
                        {
                            btnZhongdui.Visible = true;
                            txtZhongdui.Visible = true;
                            txtZhongdui.Text = DateTime.Now.ToString("yyyy-MM-dd");
                            txtUserNameZhongDui.Visible = true;
                        }

                        if (string.IsNullOrEmpty(record.DZDOk.Trim()))
                        {
                            btnDaDui.Visible = true;
                            txtDaDui.Visible = true;
                            txtDaDui.Text = DateTime.Now.ToString("yyyy-MM-dd");
                            txtUserNameDaDui.Visible = true;
                        }

                        if (record.ZhiDOK == null || record.ZhiDOK == "")
                        {

                            btnZhiDui.Visible = true;
                            txtDateZhiDui.Visible = true;
                            txtDateZhiDui.Text = DateTime.Now.ToString("yyyy-MM-dd");
                            txtUserNameZhiDui.Visible = true;
                        }

                    }
                }
            }

            lblVname.Text = VehicleName;
            if (flag == 0)
            {//车辆损坏
                MendRecordInfo record = MendRecord.GetDetail(id);
                lblInfo.Text = record.Remark;
                ZhiDuOk.Text = record.ZhiDOK;
                ZhongDuOk.Text = record.ZhongdOk;
                DaDuOk.Text = record.DZDOk;
            }
            else
            {//装备损坏
                MendDeviceInfo menddevice = MendDevice.GetDetail(id);
                DeviceInfo device = Device.GetDevice(menddevice.DeviceId);

                string strCount = GetDeviceCountUnit(menddevice);
                lblInfo.Text = device.DeviceName + "&nbsp;&nbsp;" + strCount + "&nbsp;&nbsp;" + menddevice.Remark;
                ZhiDuOk.Text = menddevice.ZhidOk;
                ZhongDuOk.Text = menddevice.ZhongdOk;
                DaDuOk.Text = menddevice.DadOk;
            }
        }
    }

    string GetDeviceCountUnit(MendDeviceInfo menddevice)
    {
        string strCount = menddevice.Count.ToString();
        DeviceInfo device = Device.GetDevice(menddevice.DeviceId);
        if (device != null)
        {
            Yipusoft.Common.Model.CodeItemInfo iteminfo = CodeItem.GetCodeItem(device.UnitName);
            string UnitName = "";

            if (iteminfo != null)
            {
                UnitName = iteminfo.ItemName;

            }
            string[] strCounts = strCount.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            if (strCounts.Count() > 1)
            {
                if (Convert.ToInt32(strCounts[1]) > 0)
                {
                    strCount = strCount + UnitName;
                }
                else
                {
                    strCount = strCounts[0] + UnitName;
                }
            }
            else
            {
                strCount = strCounts[0] + UnitName;
            }

            return strCount;
        }
        return strCount;
    }
    protected void btnZhiDui_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        Guid id = new Guid(Request["id"]);
        Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        string departmentName = Yipusoft.Account.BLL.Department.GetDepartment(user.DepartmentID).ShortName;
        DateTime dt = DateTime.Now;
        if (Request["Flag"] == "0")
        {
            MendRecordInfo Reco = MendRecord.GetDetail(id);                       
            
            string name = "";

            if (txtZhongdui.Visible && btn.CommandName.Equals("3"))
            {
                dt = DateTime.Parse(txtZhongdui.Text);
                name = txtUserNameZhongDui.Text;
                Reco.ZhongdOk = "<br/><br/>" + ddlDepartment.SelectedItem.Text + "<br/>" + name + "<br/>" + dt.ToShortDateString();
            }
            if (txtDaDui.Visible&&btn.CommandName.Equals("2"))
            {
                dt = DateTime.Parse(txtDaDui.Text);
                name = txtUserNameDaDui.Text;
                Reco.DZDOk = "<br/><br/>" + ddlDepartment.SelectedItem.Text + "<br/>" + txtUserNameDaDui.Text + "<br/>" + dt.ToShortDateString();
            }
            if (txtDateZhiDui.Visible&&btn.CommandName.Equals("1"))
            {
                dt = DateTime.Parse(txtDateZhiDui.Text);
                name = txtUserNameZhiDui.Text;
                Reco.ZhiDOK = "<br/><br/>" + ddlDepartment.SelectedItem.Text + "<br/>" + name + "<br/>" + dt.ToShortDateString();
            }


            //Reco.ZhongdOk += Reco.ZhongdOk == "" ? "" : "<br/><br/>";
            //Reco.ZhongdOk += departmentName + "<br/>" + user.UserName + "<br/>" + dt.ToShortDateString();
            Reco.FinishTime = dt;
            MendRecord.Update(Reco);
        }
        if (Request["Flag"] =="1")
        {
            MendDeviceInfo menddevice = MendDevice.GetDetail(id);
            string name = "";

            if (txtZhongdui.Visible && btn.CommandName.Equals("3"))
            {
                dt = DateTime.Parse(txtZhongdui.Text);
                name = txtUserNameZhongDui.Text;
                menddevice.ZhongdOk = "<br/><br/>" + ddlDepartment.SelectedItem.Text + "<br/>" + name + "<br/>" + dt.ToShortDateString();
            }
            if (txtDaDui.Visible && btn.CommandName.Equals("2"))
            {
                dt = DateTime.Parse(txtDaDui.Text);
                name = txtUserNameDaDui.Text;
                menddevice.DadOk = "<br/><br/>" + ddlDepartment.SelectedItem.Text + "<br/>" + name + "<br/>" + dt.ToShortDateString();
            }
            if (txtDateZhiDui.Visible && btn.CommandName.Equals("1"))
            {
                dt = DateTime.Parse(txtDateZhiDui.Text);
                name = txtUserNameZhiDui.Text;
                menddevice.ZhidOk = "<br/><br/>" + ddlDepartment.SelectedItem.Text + "<br/>" + name + "<br/>" + dt.ToShortDateString();
            }


            //Reco.ZhongdOk += Reco.ZhongdOk == "" ? "" : "<br/><br/>";
            //Reco.ZhongdOk += departmentName + "<br/>" + user.UserName + "<br/>" + dt.ToShortDateString();
            menddevice.FinishTime = dt;
            MendDevice.Update(menddevice);
            
        }

    }
}
